package main

import "fmt"

type ListNode struct {
	Val  int
	Next *ListNode
}

//func removeNthFromEnd(head *ListNode, n int) *ListNode {
////	temp := head
////	count:=0
////	for   {
////		if temp.Next==nil {
////			break
////		}
////		count++
////		temp=temp.Next
////	}
////	temp=head.Next
////	target:=count-n
////	if count==1{
////		head.Next=nil
////		fmt.Println(count)
////		return head
////	}
////	for i:=0;i<target ;i++  {
////		if i+1==target &&temp.Next.Next!=nil{
////			temp.Next=temp.Next.Next
////			break
////		}else if i+1==target&&temp.Next.Next==nil {
////			temp.Next=nil
////			break
////		}
////		temp=temp.Next
////	}
////
////	return head
////}
func getLength(head *ListNode) (length int) {
	for ; head != nil; head = head.Next {
		length++
	}
	return
}

func removeNthFromEnd(head *ListNode, n int) *ListNode {
	length := getLength(head)
	fmt.Println(length)
	dummy := &ListNode{0, head}
	cur := dummy
	for i := 0; i < length-n; i++ {
		cur = cur.Next
	}
	cur.Next = cur.Next.Next
	return dummy.Next
}
func main() {
	head := &ListNode{}
	node1 := &ListNode{1, nil}
	node2 := &ListNode{2, nil}
	node3 := &ListNode{3, nil}
	node4 := &ListNode{4, nil}
	node5 := &ListNode{5, nil}
	head.Next = node1
	node1.Next = node2
	node2.Next = node3
	node3.Next = node4
	node4.Next = node5
	temp := removeNthFromEnd(head, 1).Next
	//temp:=head.Next
	for {
		if temp == nil {
			break
		}
		fmt.Printf("[%d]=>", temp.Val)
		temp = temp.Next
	}
}
